import QtQuick 2.8
import QtQuick.Controls 2.1
import QtQuick.Layouts 1.2


Page {
    id: pageTaskCounter2
    title: qsTr("帐号")
    visible: true
 
    property StackView stack: null
    
    // property int currentIndex: 1
    property int currentIndex: rootWindow.batchIndex
    property int counterValue: 0
    property alias text:counterDisplay.text

    function counterSubmit(next){
        if(counterValue <= 0){
            rootWindow.popupTips('无效的计数') //弹窗提示
            return;
        }

        rootWindow.popupShowLoading()
        var res = gHttp.countSubmit(rootWindow.posId, rootWindow.batchName, currentIndex, counterValue, 1);
        rootWindow.popupHideLoading()

        var json = JSON.parse(res);
        
        //提示后清空计数
        rootWindow.popupTips(json.message, 1000, function(){
            counterDisplay.text = '';
            counterValue = 0;
            
            if(json.code >=0 && next){ //是否切换考场
                addIndex()
            }
        })
    }

    function judgeNumber(){
        if(parseInt(text) > 0 & parseInt(text) < 18){
            // console.log('手动成功上报：'+ counterDisplay.text+'数量')
            counterValue = counterDisplay.text;
            counterSubmit(false)
            // TODO 延时
            timerOnce(1000, function(){
                stack.custPop()
                stack.custPop()
                })
        }else if(parseInt(text) >= 18){
            rootWindow.popupTips('超过手动上报上限：18张')
            counterDisplay.text = ''
        }
    }
    // 延迟函数
    function timerOnce(msec, functor) { //https://blog.csdn.net/nicai_xiaoqinxi/article/details/125379925
        var timer = Qt.createQmlObject('import QtQuick 2.0; Timer {running: true;}',
                                    pageTaskCounter2,
                                    "MyTimer");
        var func = function() {
            functor()
            timer.destroy()
        }
        timer.interval = msec
        timer.onTriggered.connect(func)
        return timer
    }


    Keys.enabled: true
    focus: true
    Keys.onPressed: {
        if(event.key == Qt.Key_Escape) {
            stack.custPop()
            stack.custPop()
            return;
        }
       
        if(event.key == Qt.Key_F1){
            judgeNumber();
        }
        event.accepted = true
    }

        
    // 页头
    header: ToolBar {
        background: Rectangle {
            implicitWidth: 100
            implicitHeight: 40
            opacity: 1
            color: "#3f51b5"
        }
        RowLayout {
            anchors.fill: parent
            ToolButton {
                text: qsTr("‹返回[电源键]")
                onClicked: stack.custPop()
            }
            Label {
                text: "手动上报计数"
                font.pointSize: 16
                color: '#ffffff'
                elide: Label.ElideRight
                horizontalAlignment: Qt.AlignHCenter
                verticalAlignment: Qt.AlignVCenter
                Layout.fillWidth: true
            }
        }
    }
    // 页尾
    footer: ToolBar {
        background: Rectangle {
            implicitWidth: 100
            implicitHeight: 40
            opacity: 1
            color: "#3f51b5"
        }
        RowLayout {
            anchors.fill: parent
            Label {
                text: "题卡张数[0~9]，确认提交[计数]"
                font.pointSize: 16
                color: '#ffffff'
                elide: Label.ElideRight
                horizontalAlignment: Qt.AlignHCenter
                verticalAlignment: Qt.AlignVCenter
                Layout.fillWidth: true
            }
        }
    }
    // 主体
    Column{
        anchors.fill: parent
        // 考试名称
        Rectangle{
            width: parent.width
            height: 30
            color: '#2c489c'
            Text{
                width: parent.width
                height: parent.height
                // horizontalAlignment: Text.AlignHCenter//(1)
                verticalAlignment: Text.AlignVCenter    //垂直居中，控件必须有height才可以使用
                horizontalAlignment: Text.AlignLeft
                font.pointSize: 20
                font.bold: true
                color: 'white'
                text:'考试名称：' + rootWindow.examName
            }
        }
        // 考点名称
        Rectangle{
            width: parent.width
            height: 30
            color: '#2c489c'
            Text{
                width: parent.width
                height: parent.height
                // horizontalAlignment: Text.AlignHCenter//(1)
                verticalAlignment: Text.AlignVCenter    //垂直居中，控件必须有height才可以使用
                horizontalAlignment: Text.AlignLeft
                font.pointSize: 20
                font.bold: true
                color: 'white'
                text:'考点名称：' + rootWindow.posName
            }
        }
        // 考试批次
        Rectangle{
            width: parent.width
            height: 30
            color: '#2c489c'
            Text{
                width: parent.width
                height: parent.height
                // horizontalAlignment: Text.AlignHCenter//(1)
                verticalAlignment: Text.AlignVCenter    //垂直居中，控件必须有height才可以使用
                horizontalAlignment: Text.AlignLeft
                font.pointSize: 20
                font.bold: true
                color: 'white'
                text: '批次：' + rootWindow.batchName + ', 考场总数：' + rootWindow.batchCount
            }
        }
        // 考场号
        Rectangle{
            width: parent.width
            height: 80
            color: '#2c489c'
            Text{
                id: examIndex
                width: parent.width
                height: 80
                // horizontalAlignment: Text.AlignHCenter//(1)
                verticalAlignment: Text.AlignVCenter    //垂直居中，控件必须有height才可以使用
                horizontalAlignment: Text.AlignHCenter
                font.pointSize: 30
                font.bold: true
                color: '#91d145'
                text: `当前第：${currentIndex} 考场`
            }
        }
        // 答题卡数量纯文本
        Rectangle{
            width: parent.width
            height: 80
            color: '#2c489c'
            Text{
                width: parent.width
                height: 80
                // horizontalAlignment: Text.AlignHCenter//(1)
                verticalAlignment: Text.AlignVCenter    //垂直居中，控件必须有height才可以使用
                horizontalAlignment: Text.AlignHCenter
                font.pointSize: 20
                font.bold: true
                color: '#91d145'
                text:'答题卡数量'
            }
        }
        // 答题卡申报输入框
        Rectangle{
            width: parent.width
            color: '#2c489c'
            height: parent.height - 90
            TextInput{
                id: counterDisplay
                width: parent.width
                height: 50
                // horizontalAlignment: Text.AlignHCenter//(1)
                verticalAlignment: Text.AlignVCenter    //垂直居中，控件必须有height才可以使用
                horizontalAlignment: Text.AlignHCenter
                font.pointSize: 50
                font.bold: true
                focus:true
                color: '#91d145'
                text:''
            }
        }
    }

    // 组件初始化时候，自动加载
    Component.onCompleted:{
        // console.log("输入的考场号为：" + rootWindow.batchIndex)
        counterDisplay.text = ''

    }
    Component.onDestruction:{
        // console.log("onDestruction")
        // stack.custPush('PageTaskD.qml')
    }
    
}